{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pymysql\n",
    "import jieba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['672981058', '290372201', '887997105', '545384211', '972982368', '672988487', '205430684', '672756810', '587929620', '247977835', '205472783', '887997105', '417943498', '672844804', '375432715', '247997928', '972995357', '587969375', '802934945', '887834112', '417960044', '375487315', '887819067', '460498130', '247815161', '802970900', '802880914', '332905144', '930431171', '247937692', '375452051', '332922333', '417983744', '845449064', '760425621', '205411207', '802963270', '502971234', '630375962', '247937692', '545476263', '845332679', '247954746', '757994895', '205396001', '845371318', '545454307', '247881749', '332936171', '672915241', '630390320', '332936171', '375461117', '332997676', '502958341', '417880800', '290460767', '290418422', '630487616', '375461117', '205456219', '930293735', '715260245', '930310137', '545336222', '887907538', '672952856', '290454682', '930457229', '930457229', '930415802', '460402676', '845341389', '332945268', '247971582', '545388887', '760452879', '760381076', '715381126', '930415802', '375270376', '332964901', '715412935', '332953210', '845470552', '332981699', '630482641', '205408299', '417902684', '460473914', '930465645', '290400725', '460406858', '887949982', '887928999', '715337603', '760380647', '930464655', '672880811', '375427803']\n"
     ]
    }
   ],
   "source": [
    "def getOidData():\n",
    "    try:\n",
    "        conn = pymysql.connect(host=\"localhost\",\n",
    "                           port = 3306,\n",
    "                           user = \"root\",\n",
    "                           password = \"1234\",\n",
    "                           db = \"bc\")\n",
    "        cursor = conn.cursor()\n",
    "        sql = \"\"\"\n",
    "            select oid from hot_content;\n",
    "        \"\"\"\n",
    "        cursor.execute(sql)\n",
    "        oids = cursor.fetchall()\n",
    "        return [oid[0] for oid in oids]\n",
    "    except Exception as e:\n",
    "        print(\"出错了\", e)\n",
    "    finally:\n",
    "        if conn:\n",
    "            conn.close()\n",
    "        if cursor:\n",
    "            cursor.close()\n",
    "\n",
    "print(getOidData())\n",
    "\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "def getConnAndCursor():\n",
    "     try:\n",
    "        conn = pymysql.connect(host=\"localhost\",\n",
    "                           port = 3306,\n",
    "                           user = \"root\",\n",
    "                           password = \"1234\",\n",
    "                           db = \"bc\")\n",
    "        cursor = conn.cursor()\n",
    "        return (conn ,cursor)\n",
    "     except Exception as e:\n",
    "         print(\"链接出错了\", e)\n",
    "         return None\n",
    "\n",
    "\n",
    "def closeConnAndCursor(conn, cursor):\n",
    "    conn.close\n",
    "    cursor.close"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "def getHead500Content(tableName):\n",
    "    conn, cursor = getConnAndCursor()\n",
    "    sql = \"\"\"\n",
    "        SELECT lmessage FROM `bc`.`{}` LIMIT 0,500\n",
    "    \"\"\".format(tableName)\n",
    "    cursor.execute(sql)\n",
    "    data = cursor.fetchall()\n",
    "    closeConnAndCursor(conn, cursor)\n",
    "    # print(data)\n",
    "    data = [d[0] for d in data]\n",
    "    return data\n",
    "\n",
    "def concatStr(oid):\n",
    "    test = \"\"\n",
    "    data = getHead500Content(oid)\n",
    "    data = test.join(data)\\\n",
    "        .replace(\"。\",\"\")\\\n",
    "        .replace(\"：\",\"\")\\\n",
    "        .replace(\"[\",\"\")\\\n",
    "        .replace(\"]\",\"\")\\\n",
    "        .replace(\"？\",\"\")\\\n",
    "        .replace(\"，\", \"\")\n",
    "    return data\n",
    "\n",
    "\n",
    "def cutWords(oid):\n",
    "    wordFrequent = {}\n",
    "    data = jieba.lcut(concatStr(oid))\n",
    "    # print(data)\n",
    "    for d in data:\n",
    "        if d not in wordFrequent and len(d) != 1:\n",
    "            wordFrequent[d] = data.count(d)\n",
    "    return wordFrequent\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "def saveCutWord(oid, data):\n",
    "    conn, cursor = getConnAndCursor()\n",
    "    sql = \"\"\"\n",
    "        UPDATE `bc`.`hot_content` SET `word_frequent` = '{}' WHERE `oid` = '{}'\n",
    "    \"\"\".format(data, oid)\n",
    "    cursor.execute(sql)\n",
    "    conn.commit()\n",
    "    closeConnAndCursor(conn, cursor)\n",
    "\n",
    "\n",
    "def getWordFrequent1(oid):\n",
    "    cutwords = cutWords(oid)\n",
    "    data = sorted(cutwords.items(), key=lambda cutwords: cutwords[1], reverse=True)[:50]\n",
    "    for d in data:\n",
    "        sd = d[0] + \":\" + str(d[1])\n",
    "    data = [d[0] + \":\" + str(d[1]) for d in data]\n",
    "    data = \"|\".join(data)\n",
    "    # print(data)\n",
    "    saveCutWord(oid, data)\n",
    "    # print(dict(data))\n",
    "    # print(type(data))\n",
    "\n",
    "def getWordFrequent(oid):\n",
    "    cutwords = cutWords(oid)\n",
    "    data = sorted(cutwords.items(), key=lambda cutwords: cutwords[1], reverse=True)[:50]\n",
    "    # print(data)\n",
    "    saveCutWord(oid, data)\n",
    "    # print(dict(data))\n",
    "    # print(type(data))\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "a = \"我\"\n",
    "print(len(a))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache D:\\Personal\\Temp\\jieba.cache\n",
      "Loading model cost 1.113 seconds.\n",
      "Prefix dict has been built successfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "执行成功！\n"
     ]
    }
   ],
   "source": [
    "getWordFrequent(672981058)\n",
    "# oids = getOidData()\n",
    "# for oid in oids:\n",
    "#     getWordFrequent(oid)\n",
    "print(\"执行成功！\")"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}